﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.enprise
{
    public partial class eforget_pwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                string email = this.txtemail.Text.Trim();
                string checkcode = this.txtCode.Text.Trim();
                if (Session["CheckCode"] != null)
                {
                    if (checkcode.ToUpper() == Session["CheckCode"].ToString())
                    {
                        lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();

                        DataSet ds = eb.FindIDByEmail(email);
                        if (ds != null && ds.Tables[0].Rows.Count != 0)
                        {
                            string dtNow = getUnixTime(DateTime.Now);

                            string uid = ds.Tables[0].Rows[0]["enterprise_bid"].ToString();
                            string name = ds.Tables[0].Rows[0]["name"].ToString();
                            string title = "密码重置邮件 - 来自我就业网";
                            string link = "http://" + HttpContext.Current.Request.Url.Host + "/enprise/ereset_pwd.aspx?email=" + email + "&code=" + dtNow;
                            string body = "尊敬的" + name + ", 你好：<br />";

                            body += "您可以点击下面的链接，来重新设置密码!<br />";
                            body += "<a href='" + link + "' target=_blank style='color:#25557D'>" + link + "</a>";
                            try
                            {
                                lcmyjobs.Model.email em = new Model.email();
                                em.port = int.Parse(myPublic.GetConfig("emailHostPort"));
                                em.sendEmailAdd = myPublic.GetConfig("sendEmailAdd");
                                em.sendEmailPwd = myPublic.GetConfig("sendEmailPwd");
                                em.serverhost = myPublic.GetConfig("emailHost");
                                em.title = title;
                                em.receiveEmailAdd = email;
                                em.content = body;

                                lcmyjobs.BLL.SendEmail se = new BLL.SendEmail(em);
                                se.Send();
                                //myPublic.sendMail(email, title, body);                    
                                // 时间戳
                                eb.setResetPWDTime(uid, dtNow);
                                Maticsoft.Common.MessageBox.Show(this, "发送成功！");

                            }
                            catch (Exception ex)
                            {
                                //string msg = "发送失败！" + ex.Message;
                                string msg = "发送失败！";
                                Maticsoft.Common.MessageBox.Show(this, msg);
                            }
                        }
                        else
                        {
                            Maticsoft.Common.MessageBox.Show(this, "输入的用户不存在！");
                        }

                    }
                    else
                        Maticsoft.Common.MessageBox.Show(this, "验证码不对！");
                }
                else
                    Maticsoft.Common.MessageBox.Show(this, "验证码不对！");

            }
        }

        private string getUnixTime(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
            TimeSpan toNow = dt.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            return timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
        }
    }
}